package com.ruoyi.business.controller;

import java.io.IOException;
import java.util.List;

import com.ruoyi.business.domain.JtCountryCode;
import com.ruoyi.business.service.IJtCountryCodeService;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.utils.JtImageUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.JtBranchServices;
import com.ruoyi.business.service.IJtBranchServicesService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 服务网点Controller
 * 
 * @author Alan Zhang
 * @date 2025-03-29
 */
@Controller
@RequestMapping("/business/services")
public class JtBranchServicesController extends BaseController
{
    private String prefix = "business/services";

    @Autowired
    private IJtBranchServicesService jtBranchServicesService;
    @Autowired
    private IJtCountryCodeService jtCountryCodeService;

    @RequiresPermissions("business:services:view")
    @GetMapping()
    public String services()
    {
        return prefix + "/services";
    }

    /**
     * 查询服务网点列表
     */
    @RequiresPermissions("business:services:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(JtBranchServices jtBranchServices)
    {
        startPage();
        List<JtBranchServices> list = jtBranchServicesService.selectJtBranchServicesList(jtBranchServices);
        return getDataTable(list);
    }

    /**
     * 导出服务网点列表
     */
    @RequiresPermissions("business:services:export")
    @Log(title = "服务网点", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JtBranchServices jtBranchServices)
    {
        List<JtBranchServices> list = jtBranchServicesService.selectJtBranchServicesList(jtBranchServices);
        ExcelUtil<JtBranchServices> util = new ExcelUtil<JtBranchServices>(JtBranchServices.class);
        return util.exportExcel(list, "服务网点数据");
    }

    /**
     * 新增服务网点
     */
    @RequiresPermissions("business:services:add")
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        //国家地区
        mmap.put("countryCodeList", findCountry());
        return prefix + "/add";
    }

    private List<JtCountryCode> findCountry(){
        JtCountryCode countryCode = new JtCountryCode();
        countryCode.setStatus(0);
        return jtCountryCodeService.selectJtCountryCodeList(countryCode);
    }

    /**
     * 新增保存服务网点
     */
    @RequiresPermissions("business:services:add")
    @Log(title = "服务网点", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult addSave(JtBranchServices jtBranchServices,@RequestParam(value = "file", required = false)  MultipartFile file) throws IOException {
        if (file != null) {
            jtBranchServices.setImageUrl(JtImageUtil.uploadBranchService(file, true));
        }
        return toAjax(jtBranchServicesService.insertJtBranchServices(jtBranchServices));
    }

    /**
     * 修改服务网点
     */
    @RequiresPermissions("business:services:edit")
    @GetMapping("/edit/{servicesId}")
    public String edit(@PathVariable("servicesId") Long servicesId, ModelMap mmap)
    {
        //国家地区
        mmap.put("countryCodeList", findCountry());
        JtBranchServices jtBranchServices = jtBranchServicesService.selectJtBranchServicesByServicesId(servicesId);
        mmap.put("jtBranchServices", jtBranchServices);
        return prefix + "/edit";
    }

    /**
     * 修改保存服务网点
     */
    @RequiresPermissions("business:services:edit")
    @Log(title = "服务网点", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(JtBranchServices jtBranchServices,@RequestParam(value = "file", required = false)  MultipartFile file) throws IOException {
        if (file != null) {
            jtBranchServices.setImageUrl(JtImageUtil.uploadBranchService(file, true));
        }
        return toAjax(jtBranchServicesService.updateJtBranchServices(jtBranchServices));
    }

    /**
     * 删除服务网点
     */
    @RequiresPermissions("business:services:remove")
    @Log(title = "服务网点", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(jtBranchServicesService.deleteJtBranchServicesByServicesIds(ids));
    }
}
